{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# spu召回程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import defaultdict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 流行度召回\n",
    "* 首先得到训练集中每个菜品的被购买的次数，然后按照次数排序，分别选择5万，2万个菜品作为每个用户的召回物品。\n",
    "* 这个方法不用区分每个用户的特点，实现方便，简单。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "182753\n"
     ]
    }
   ],
   "source": [
    "def zero():\n",
    "    return 0\n",
    "spu_popularity = defaultdict(zero)\n",
    "\n",
    "for line in open('./data/me/submit_data/train.txt'):\n",
    "    for spu in line.strip('\\n').split(' ')[1:]:\n",
    "        spu_popularity[spu] += 1\n",
    "\n",
    "print(len(spu_popularity))\n",
    "\n",
    "recall_N = 20000 # 召回的数量\n",
    "\n",
    "S = sorted(spu_popularity.items(),key=lambda item:item[1],reverse=True)[:recall_N]\n",
    "\n",
    "with open('recall_items_2w.txt','w') as f:\n",
    "    for s in S:\n",
    "        f.write(s[0])\n",
    "        f.write('\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**上面一段代码需要运行两遍，分别修改recall_N=20000,recall_N=50000和recall_items_2w.txt，recall_items_5w.txt。生成召回2万和5万的菜品**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
